package com.example.musicdemo.ui.activity

import android.content.Intent
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.widget.ImageView

import androidx.appcompat.widget.Toolbar
import androidx.fragment.app.FragmentTransaction
import com.example.musicdemo.R
import com.example.musicdemo.base.BaseActivity
import com.example.musicdemo.service.AudioService
import com.example.musicdemo.util.FragmentUtil

import kotlinx.android.synthetic.main.activity_main.*
import org.jetbrains.anko.find
import org.jetbrains.anko.toast

class MainActivity : BaseActivity(), View.OnClickListener {
    private val mToolbar: Toolbar by lazy {
        find(R.id.toolbar)
    }


    private lateinit var mIvCur: ImageView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        setSupportActionBar(mToolbar)
        initHome()

        rl_home.setOnClickListener(this)
        rl_mv.setOnClickListener(this)
        rl_ybang.setOnClickListener(this)
        rl_yuedan.setOnClickListener(this)

    }

    override fun getLayoutId(): Int {
        return R.layout.activity_main
    }




    /**
     * 设置toolbar菜单的
     */
    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.main, menu)
        return super.onCreateOptionsMenu(menu)
    }


    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            R.id.item_setting -> this.startActivity(
                Intent(
                    mToolbar.context,
                    SettingActivity::class.java
                )
            )

            R.id.item_search ->this.startActivity(Intent(mToolbar.context,SearchActivity::class.java))
        }
        return true
    }


    override fun initListener() {
/*        mBottomBar.setOnTabSelectListener {

            val transaction = supportFragmentManager.beginTransaction()
            transaction.replace(
                R.id.container, FragmentUtil.fragmentUtil.getFragment(it)!!, it.toString()
            )
            transaction.commit()
        }*/




    }


    override fun onClick(v: View?) {


        mIvCur.isSelected = false
        when (v?.id) {
            R.id.rl_home -> {
                mIvCur = iv_home
                mIvCur.isSelected = true
            }
            R.id.rl_mv -> {
                mIvCur = iv_mv
                mIvCur.isSelected = true

            }
            R.id.rl_ybang -> {
                mIvCur = iv_ybang
                mIvCur.isSelected = true
            }
            R.id.rl_yuedan -> {

                mIvCur = iv_yuedan
                mIvCur.isSelected = true
            }

        }
        if (v?.id == R.id.rl_home || v?.id == R.id.rl_mv || v?.id == R.id.rl_ybang || v?.id == R.id.rl_yuedan) doTransaction(v.id)
    }


    /**
     * 获取transaction 替换fragment  并且commit
     */
    private fun doTransaction(id:Int){
        val transaction = supportFragmentManager.beginTransaction()
        transaction.replace(
            R.id.container, FragmentUtil.fragmentUtil.getFragment(id)!!, id.toString()
        )
        transaction.commit()

    }


    private fun initHome(){
        mIvCur = iv_home
        mIvCur.isSelected = true
        doTransaction(R.id.rl_home)
    }

}

